<?php
  require_once 'DB.php';
  include_once("./libraries/db.inc.php");
  include_once("./libraries/authentication.inc.php");
  include_once("./queries/machine.inc.php");
  include_once("./libraries/util.inc.php");
  include_once("./libraries/displayUtils.inc.php");
  include_once("./libraries/recordset.inc.php");

  session_start();
  sessionAuthenticate("login.php");

  $dataIndex = "machineList";
  
  $refresh = false;
  $queryType = -1;
  $params = array();
  //$before = null;
  $before_scalar = null;
  $before_unit = null;
  //$after = null;
  $after_scalar = null;
  $after_unit = null;
  $displayPage = 1;
	//below is a hack to fix incase we scroll back to 1
  $pageSet = false;

  //process the get variables
  foreach ($_GET as $varName => $value) {
		  //echo "varName: {$varName}, value: {$value}\n";
	  if ($varName == "refresh") {
		  $refresh=$value;
	  } elseif ($varName == "qt") {
		  $queryType = $value;
	  } elseif ($varName == "before_scalar") {
		  $before_scalar = $value;
	  } elseif ($varName == "before_unit") {
		  $before_unit = $value;
	  } elseif ($varName == "after_scalar") {
		  $after_scalar = $value;
	  } elseif ($varName == "after_unit") {
		  $after_unit = $value;
	  } elseif ($varName == "dP") {
		  $displayPage = $value;
                  $pageSet = true;
	  } else {
          $params["{$varName}"] = $value;
	  } 
  }

  if (count($params) == 0) $params = $_SESSION["{$dataIndex}"]["params"];
  
  // Won't the last statement always be true because of || true ?
  //if (!isset($before_scalar) && !isset($after_scalar) && 
  //    ($displayPage > 1 || $pageSet == true || true)) {

  // Took out the || true
  if (!isset($before_scalar) && !isset($after_scalar) && 
      ($displayPage > 1 || $pageSet == true)) {
		  // if we are displaying successive pages in a report
		  // get the following arguments from session state.
	  $before_scalar = $_SESSION["{$dataIndex}"]["before_scalar"];
	  $before_unit = $_SESSION["{$dataIndex}"]["before_unit"];
	  $after_scalar = $_SESSION["{$dataIndex}"]["after_scalar"];
	  $after_unit = $_SESSION["{$dataIndex}"]["after_unit"];
  }

  //check for from and to range
  if (!isset($before_scalar) && !isset($after_scalar)) {
     //echo "should do it";
     $after_scalar = MACHINE_CLASSAD_VALID_INTERVAL_SCALAR;
     $after_unit = MACHINE_CLASSAD_VALID_INTERVAL_UNIT;
  }
  //echo "now after is $after";

    //we need to get the data
    $connection = DB::connect($dsn, $options);
    if (DB::isError($connection)) {
      trigger_error($connection->getMessage(), E_USER_ERROR);
    }

  //now we know we have data, so display it
  $displayPage=1;
  if (isset($_GET["dP"])) {
    $displayPage = (int)($_GET["dP"]);
  }

//echo "before is $before";
//echo "after is $after";

    //echo "right now after is $after";
   $rs = machineList($connection, $dataIndex, $params, $before_scalar, $before_unit, $after_scalar, $after_unit, $displayPage);
    //echo "called it";

    $connection->disconnect();
 
  $rs->setLinkParams(0, array("mid"), array(0), "machineInfo.php");
  $rs->setLinkParams(3, array("schedd", "cid", "pid"), array(1,2,3), "procInfo.php");
  //$rs->simpleSetLinkParams(0, "mid");
  //$rs->linkFile = "machineInfo.php";
  //echo "Size is now: {$rs->recordCount}";
  $rs->printNavigablePage($_SESSION["{$dataIndex}"]["title"],
                          $_SESSION["{$dataIndex}"]["message"],
                          $displayPage,
                          "./machineList.php");
  cdbFinishPage();

?>
